<script setup>
  import PageContainer from '@/components/PageContainer.vue'
  import { ref } from 'vue'
  import { userUpdatePassword } from '@/api/user'
  import { useUserStore } from '@/stores'
  import { useRouter } from 'vue-router'

  // import { ElMessage } from 'element-plus'
  const userStore = useUserStore()
  const formRef = ref()
  const formModle = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: ''
  })
  const rules = {
    old_pwd: [
      { required: true, message: '请输入旧密码', trigger: 'blur' },
      {
        pattern: /^\w{6,15}$/,
        message: '请输入6-15位的非空字符串',
        trigger: 'blur'
      }
    ],
    new_pwd: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      {
        pattern: /^\w{6,15}$/,
        message: '请输入6-15位的非空字符串',
        trigger: 'blur'
      },
      {
        validator: (rule, value, callback) => {
          if (formModle.value.old_pwd === value) {
            callback(new Error('新密码与旧密码重复'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    re_pwd: [
      { required: true, message: '请输入确认密码', trigger: 'blur' },
      {
        pattern: /^\w{6,15}$/,
        message: '请输入6-15位的非空字符串',
        trigger: 'blur'
      },
      {
        validator: (rule, value, callback) => {
          if (formModle.value.new_pwd !== value) {
            callback(new Error('新密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  }
  const router = useRouter()
  // 发送更改请求
  const changPassword = async () => {
    await formRef.value.validate()
    const res = await userUpdatePassword(formModle.value)
    console.log(res)
    ElMessage.success('修改成功')
    // 清空个人信息，退回登陆页面
    userStore.setToken('')
    userStore.removeuser({})
    router.push('/login')
  }

  // 重置密码
  const resetform = () => {
    formRef.value.resetFields()
  }
</script>
<template>
  <PageContainer title="修改密码">
    <el-row>
      <el-col :span="12">
        <el-form ref="formRef" :model="formModle" :rules="rules">
          <el-form-item label="旧密码" prop="old_pwd">
            <el-input pattern="^\S+$" v-model="formModle.old_pwd"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input
              pattern="^\S+$"
              type="password"
              v-model="formModle.new_pwd"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="re_pwd">
            <el-input
              pattern="^\S+$"
              type="password"
              v-model="formModle.re_pwd"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="changPassword" type="primary">
              修改密码
            </el-button>
            <el-button @click="resetform" type="primary">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </PageContainer>
</template>
<style lang="scss" scoped>
  :deep {
    .el-form-item__label {
      width: 80px;
      justify-content: left;
      margin-left: 0;
    }
  }
</style>
